package com.yango.teach.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yango.serviceutil.exception.YangoException;
import com.yango.teach.entity.Chapter;
import com.yango.teach.entity.Video;
import com.yango.teach.entity.vo.course.ChapterVo;
import com.yango.teach.entity.vo.course.VideoVo;
import com.yango.teach.mapper.ChapterMapper;
import com.yango.teach.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yango.teach.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author LiDingYin
 * @since 2021-07-24
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;

    //课程章节小节列表-嵌套
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //最终要返回的数据列表
        ArrayList<ChapterVo> chapterVoList = new ArrayList<>();
        //查询章节信息
        QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
        //根据课程id查询
        chapterQueryWrapper.eq("course_id", courseId);
        chapterQueryWrapper.orderByAsc("sort", "id");//根据排序和id倒序排序
        //调用方法查询
        List<Chapter> chapterList = baseMapper.selectList(chapterQueryWrapper);

        //获取小节信息
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        //根据课程id查询
        videoQueryWrapper.eq("course_id", courseId);
        videoQueryWrapper.orderByAsc("sort", "id");//根据排序和id排序
        //调用方法查询
        List<Video> videoList = videoService.list(videoQueryWrapper);

        //封装章节信息
        for (Chapter chapter : chapterList) {
            //创建章节封装对象
            ChapterVo chapterVo = new ChapterVo();
            //将chapter的值复制到chapterVo中
            BeanUtils.copyProperties(chapter, chapterVo);
            //将chapterVo放到最终返回的集合中
            chapterVoList.add(chapterVo);
            //封装小节信息
            //小节集合
            ArrayList<VideoVo> videoVoList = new ArrayList<>();
            for (Video video : videoList) {
                System.out.println("**************************" + video);
                //判断当前小节的章节id是否和章节id一样
                if (video.getChapterId().equals(chapterVo.getId())) {
                    //创建小节封装对象
                    VideoVo videoVo = new VideoVo();
                    //将video的值复制到videoVo中
                    BeanUtils.copyProperties(video, videoVo);
                    //设置是否付费
                    videoVo.setFree(video.getIsFree());
                    //将videoVo放到小节集合中
                    videoVoList.add(videoVo);
                }
            }
            //将videoVoList放到章节对象中
            chapterVo.setChildren(videoVoList);
        }
        //返回数据
        return chapterVoList;
    }

    //删除章节
    @Override
    public boolean removeChapterById(String id) {
        //根据id查询该章节下是否有小节，如果有，提示不能删除
        boolean flag = videoService.getCountByChapterId(id);
        if (flag) {
            //抛异常
            //throw new YangoException(20001, "该章节下尚有小节，请先删除小节!");
            return false;
        }
        //返回
        return this.removeById(id);
    }

    //根据课程id删除章节
    @Override
    public boolean removeByCourseId(String courseId) {
        //条件构造器
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        //调用方法删除
        boolean remove = this.remove(wrapper);
        return remove;
    }
}
